package eerbaar.client.internal.ctr.pet;

import eerbaar.client.ctr.IPersonaCmd;
import eerbaar.client.exp.ICmdFactory;
import eerbaar.client.exp.IExp;
import eerbaar.client.internal.ctr.cmd.PersonaCmdFactory;
import eerbaar.game.IGame;
import eerbaar.memory.IMemory;
import eerbaar.persona.IPersona;
import eerbaar.shared.tree.INode;
import eerbaar.shared.tree.IPayload;

public class PersonaLearnedCmdFactory implements ICmdFactory {

    private final IGame game;

    private final IPersona persona;

    public PersonaLearnedCmdFactory(IGame game, IPersona persona) {
        this.persona = persona;
        this.game = game;
    }

    @Override
    public IExp getCmd(String name) {

        IPersonaCmd temp = PersonaCmdFactory.getCmd(name);

        if (temp != null) {
            return new PersonExpression(this.game, this.persona, temp);
        }

        return null;
    }

    @Override
    public String getLearnedValue(String name) {

        String result = null;

        IMemory memory = this.persona.getHead().getBrain().getMemory();
        INode node = memory.getIdeaNode();
        INode[] childeren = node.getChildren();
        for (INode child : childeren) {

            IPayload payload = child.getPayload();
            if (payload.getKey().equals(name)) {
                result = payload.getValue();
                break;
            }
        }

        return result;
    }

    @Override
    public boolean isLearnedCmd(String name) {

        return (getLearnedValue(name) != null);
    }

    @Override
    public String toString() {

        return "factory(" + this.persona + ")";
    }
}
